BUILD:=./build
SRC:=./src
# ENTRYPOINT:=0x10000

# CFLAGS:= -m32
# CFLAGS+= -fno-builtin
# CFLAGS+= -fno-pic
# CFLAGS+= -fno-pie
# CFLAGS+= -fno-stack-protector

CFLAGS := -mcmodel=large -fno-builtin -m64 -fno-pic
# CFLAGS+= -nostdinc
# CFLAGS+= -nostdlib
CFLAGS:=$(strip ${CFLAGS})

DEBUG:= -g
INCLUDE:= -I$(SRC)/include

$(BUILD)/bootloader/%.bin: $(SRC)/boot/%.asm
	$(shell mkdir -p $(dir $@))
	nasm -f bin $< -o $@

# $(BUILD)/%.o: $(SRC)/%.asm
# 	$(shell mkdir -p $(dir $@))
# 	nasm -f elf32 $(DEBUG) $< -o $@

$(BUILD)/%.o: $(SRC)/%.c
	$(shell mkdir -p $(dir $@))
	gcc $(CFLAGS) $(DEBUG) $(INCLUDE) -c $< -o $@

$(BUILD)/%.o: $(SRC)/%.S
	$(shell mkdir -p $(dir $@))
	gcc -E $< > $(addsuffix ".s", $(basename $@))
	as --64 -o $@ $(addsuffix ".s", $(basename $@))

$(BUILD)/bootloader/%.bin: $(SRC)/bootloader/%.asm
	$(shell mkdir -p $(dir $@))
	nasm $< -o $@

$(BUILD)/system: \
			$(BUILD)/kernel/head.o \
			$(BUILD)/kernel/main.o \
			$(BUILD)/kernel/printk.o \
			$(BUILD)/kernel/font.o \
			$(BUILD)/kernel/trap.o \
			$(BUILD)/kernel/entry.o \
			$(BUILD)/kernel/memory.o \
			$(BUILD)/kernel/interrupt.o \
			$(BUILD)/kernel/task.o \
			$(BUILD)/kernel/cpu.o \

	$(shell mkdir -p $(dir $@))
	ld -b elf64-x86-64 -o $(BUILD)/system $^ -T $(SRC)/kernel/Kernel.lds

$(BUILD)/kernel.bin : $(BUILD)/system
	objcopy -I elf64-x86-64 -S -R ".eh_frame" -R ".comment" -O binary $< $@

# $(BUILD)/kernel.bin: \
# 		$(BUILD)/kernel/start.o \
# 		$(BUILD)/kernel/io.o \
# 		$(BUILD)/lib/string.o \
# 		$(BUILD)/kernel/main.o \
# 		$(BUILD)/kernel/console.o \

# 	$(shell mkdir -p $(dir $@))
# 	ld -m elf_i386 -static $^ -o $@ -Ttext $(ENTRYPOINT)

# $(BUILD)/system.bin: $(BUILD)/kernel.bin
# 	objcopy -O binary $< $@

# $(BUILD)/system.map: $(BUILD)/kernel.bin
# 	nm $< | sort > $@

# $(BUILD)/master.img: $(BUILD)/boot/boot.bin \
# 	$(BUILD)/boot/loader.bin \
# 	$(BUILD)/system.bin \
# 	$(BUILD)/system.map \

# 	yes | bximage -q -hd=16 -func=create -sectsize=512 -imgmode=flat $@
# 	dd if=$(BUILD)/boot/boot.bin of=$@ bs=512 count=1 conv=notrunc
# 	dd if=$(BUILD)/boot/loader.bin of=$@ bs=512 count=4 seek=2 conv=notrunc
# 	dd if=$(BUILD)/system.bin of=$@ bs=512 count=200 seek=10 conv=notrunc



$(BUILD)/boot.img: $(BUILD)/bootloader/boot.bin $(BUILD)/bootloader/loader.bin $(BUILD)/kernel.bin
	yes | bximage -q -fd=1.44M -func=create $@
	dd if=$(BUILD)/bootloader/boot.bin of=$@ bs=512 count=1 conv=notrunc
	sudo mount $@ /mnt/x64os/ -t vfat -o loop
	sudo cp $(BUILD)/bootloader/loader.bin /mnt/x64os/
	sudo cp $(BUILD)/kernel.bin /mnt/x64os/
	sync
	sudo umount /mnt/x64os

.PHONY: clean bochscfg qemucfg bochs qemu qemug all
clean:
	rm -rf $(BUILD)/*

bochscfg:
	sed -i '249,256s/0x\([0-9a-fA-F]\)\{2\}/0xe0/' src/kernel/head.S
	sed -i 's/__FRAMEBUFFER_PHYSICAL_ADDRESS [0-9a-fx]\{10\}/__FRAMEBUFFER_PHYSICAL_ADDRESS 0xe0000000/' src/kernel/printk.c

qemucfg:
	sed -i '249,256s/0x\([0-9a-fA-F]\)\{2\}/0xfd/' src/kernel/head.S
	sed -i 's/__FRAMEBUFFER_PHYSICAL_ADDRESS [0-9a-fx]\{10\}/__FRAMEBUFFER_PHYSICAL_ADDRESS 0xfd000000/' src/kernel/printk.c

bochs: bochscfg all
	bochs -q

all: $(BUILD)/boot.img

qemu: qemucfg all
	qemu-system-x86_64 -m 2G -fda build/boot.img -cpu Haswell

qemug: all
	qemu-system-x86_64 -m 2G -fda build/boot.img -cpu Haswell -s -S